package com.ruoyi.project.system.controller;

import java.net.URLDecoder;
import java.util.List;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.KnowledgeArticle;
import com.ruoyi.project.system.service.IKnowledgeArticleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 安全常识文章Controller  安全内容
 * 
 * @author ruoyi
 * @date 2020-10-07
 */
@RestController
@RequestMapping("/system/knowledgeArticle")
public class    KnowledgeArticleController extends BaseController
{
    @Autowired
    private IKnowledgeArticleService knowledgeArticleService;

    /**
     * 查询安全常识文章列表
     */
    @PreAuthorize("@ss.hasPermi('system:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(KnowledgeArticle knowledgeArticle)
    {
        startPage();
        List<KnowledgeArticle> list = knowledgeArticleService.selectKnowledgeArticleList(knowledgeArticle);
        return getDataTable(list);
    }


    /**
     * 导出安全常识文章列表
     */
    @PreAuthorize("@ss.hasPermi('system:article:export')")
    @Log(title = "安全常识文章", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(KnowledgeArticle knowledgeArticle)
    {
        List<KnowledgeArticle> list = knowledgeArticleService.selectKnowledgeArticleList(knowledgeArticle);
        ExcelUtil<KnowledgeArticle> util = new ExcelUtil<KnowledgeArticle>(KnowledgeArticle.class);
        return util.exportExcel(list, "article");
    }

    /**
     * 获取安全常识文章详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(knowledgeArticleService.selectKnowledgeArticleById(id));
    }

    /**
     * 新增安全常识文章
     */
    @PreAuthorize("@ss.hasPermi('system:article:add')")
    @Log(title = "安全常识文章", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KnowledgeArticle knowledgeArticle)
    {
        knowledgeArticle.setContent(URLDecoder.decode(knowledgeArticle.getContent()));
        return toAjax(knowledgeArticleService.insertKnowledgeArticle(knowledgeArticle));
    }

    /**
     * 修改安全常识文章
     */
    @PreAuthorize("@ss.hasPermi('system:article:edit')")
    @Log(title = "安全常识文章", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KnowledgeArticle knowledgeArticle)
    {
        knowledgeArticle.setContent(URLDecoder.decode(knowledgeArticle.getContent()));
        return toAjax(knowledgeArticleService.updateKnowledgeArticle(knowledgeArticle));
    }

    @PutMapping("/updateReadCount")
    public AjaxResult updateReadCount(@RequestBody KnowledgeArticle knowledgeArticle){
        return toAjax(knowledgeArticleService.updateReadCount(knowledgeArticle.getId()));
    }

    /**
     * 删除安全常识文章
     */
    @PreAuthorize("@ss.hasPermi('system:article:remove')")
    @Log(title = "安全常识文章", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(knowledgeArticleService.deleteKnowledgeArticleByIds(ids));
    }


    /**
     * 获取安全常识文章目录
     */
    @PreAuthorize("@ss.hasPermi('system:article:query')")
    @GetMapping(value = "/getMlList")
    public AjaxResult getMlList()
    {
        return AjaxResult.success(knowledgeArticleService.selectKnowledgeCatList());
    }
}
